#include <REGX52.H>
#include "Delay.h"
#include "matrix.h"

/**
  * @brief  获取按下按键的键值
  * @param  无
  * @retval 按下按键的键值，没有按键按下时返回0
  *		    S1=1, S1=2, S3=3, S4=4, S5=5, S6=6, S7=7, S8=8
  *		    S9=9, S10=0, S3=3, S11=确认, S12=取消, S13=删除, S14=,
  */
unsigned char MatrixKey()
{
	unsigned char keyNumber = 0;
	
	
	//每次扫描键盘之前，先把P1全部初始化为高电平
	P1=0xFF;
	P1_3 = 0;
	//delay(20)延时进行按键按下消抖， while循环检测松手， delay(20) 按键松开消抖
	if(P1_7 == 0) {Delay(20); while(P1_7 == 0); Delay(20); keyNumber = 1;}	
	if(P1_6 == 0) {Delay(20); while(P1_6 == 0); Delay(20); keyNumber = 5;}
	if(P1_5 == 0) {Delay(20); while(P1_5 == 0); Delay(20); keyNumber = 9;}
	if(P1_4 == 0) {Delay(20); while(P1_4 == 0); Delay(20); keyNumber = 13;}
	
	
	P1=0xFF;
	P1_2 = 0;
	if(P1_7 == 0) {Delay(20); while(P1_7 == 0); Delay(20); keyNumber = 2;}
	if(P1_6 == 0) {Delay(20); while(P1_6 == 0); Delay(20); keyNumber = 6;}
	if(P1_5 == 0) {Delay(20); while(P1_5 == 0); Delay(20); keyNumber = 10;}
	if(P1_4 == 0) {Delay(20); while(P1_4 == 0); Delay(20); keyNumber = 14;}
	
	
	P1=0xFF;
	P1_1 = 0;
	if(P1_7 == 0) {Delay(20); while(P1_7 == 0); Delay(20); keyNumber = 3;}
	if(P1_6 == 0) {Delay(20); while(P1_6 == 0); Delay(20); keyNumber = 7;}
	if(P1_5 == 0) {Delay(20); while(P1_5 == 0); Delay(20); keyNumber = 11;}
	if(P1_4 == 0) {Delay(20); while(P1_4 == 0); Delay(20); keyNumber = 15;}
	
	
	P1=0xFF;
	P1_0 = 0;
	if(P1_7 == 0) {Delay(20); while(P1_7 == 0); Delay(20); keyNumber = 4;}
	if(P1_6 == 0) {Delay(20); while(P1_6 == 0); Delay(20); keyNumber = 8;}
	if(P1_5 == 0) {Delay(20); while(P1_5 == 0); Delay(20); keyNumber = 12;}
	if(P1_4 == 0) {Delay(20); while(P1_4 == 0); Delay(20); keyNumber = 16;}
	
	
	return keyNumber;
}